/* 
 * File:   cmdline.cpp
 * Author: landry
 * 
 * Created on 13 octobre 2009, 18:14
 */

#include "cmdline.h"

void usageProg(char* program_name, int status){
  if (status == EXIT_SUCCESS)
    {
    	printf("   \n");
    	printf(" User manual command %s \n", program_name);
    	printf(" %s <remote host name> [-p remote host port] [-f input file] [-t format ulaw/pcm] [-n number of copies] [-l lost rate] [-d delay] [-j jitter]\n",program_name);
    	printf(" Options :\n");
    	printf(" -f [input file] audio input file defaul audio input file is /dev/audio\n");
    	printf(" -t [audio format ulaw/pcm] audio input default format is ulaw \n");
    	printf(" -p [remote host port] specifies the remote host port if defauld not used \n");
    	printf(" -n [number of file copy] specified how many copies of the file we want to transfert\n");
    	printf(" -l [lost rate] specify the lost rate between 0 and 1 \n");
    	printf(" -d [delay] specify the delay in millisecond \n");
    	printf(" -j Enable jitter, to be used as the last parameter \n");
    	printf("   \n");
    	printf(" Written by Equipe 1 \n");
    	printf(" Course title : Quality of Service and Multimedia \n");
    	printf("   \n");
    	printf("   \n");
   }
  else
    {
    	printf(" Incorrect input argument ...\n");
    	printf(" Please try %s  -h for help. \n", program_name );
    }
  exit(status);
}


void used(char* program_name, int status){
	if (status == EXIT_SUCCESS)
    {
    	printf("   \n");
    	printf(" User manual command %s \n", program_name);
    	printf(" %s [-p local port] [-f format [ulaw/pcm]] \n",program_name);
    	printf(" Options \n");
    	printf(" -p [local port] specifies the local port on which to listen if defauld not used \n");
    	printf(" -f [format [ulaw/pcm]] specifies the output device incoding format \n");
    	
    	printf(" Written by Equipe 1 \n");
    	printf(" Course title : Quality of Service and Mutimedia \n");
    	printf("   \n");
   }
  else
    {
    	printf(" Incorrect input argument ...\n");
    	printf(" Please try %s  -h for user manual. \n", program_name );
    }
  exit(status);
	
}

/*
 * _r_host remote host name
 * _r_port remote host port
 * _input_f audio input file
 * _format audio input forma 
 * */
int parse_args(int argc, char *argv[],char* _r_host,u_short *_l_port ,char* _input_f, char* _format,int *check_input,int *nb_copies,double*lost,int *delay,int *rand){
  extern char *optarg;
  extern int optind, opterr, optopt;
  
  
  char c;
   while ((c = getopt(argc, argv, "a:f:t:p:n:l:d:j:uh")) != EOF){
    switch (c) {
      case 'h':       /* Aide */
      usageProg(argv[0], EXIT_SUCCESS);

      break;
      case 'p':
      if (!(strcmp(optarg, " "))==0){
          *_l_port = atoi(optarg);
      }
      break;
      case 'f':       /* audio input */
       if (!(strcmp(optarg, " "))==0){
          strcpy (_input_f ,optarg);
          //*check_input = -1;
        }
       break;
      case 't':       /* temps d'arret */
         if (!(strcmp(optarg, " "))==0){
         strcpy (_format,optarg);
         *check_input = -1;
      }
      break;
      case 'n':       /* temps d'arret */
         if (!(strcmp(optarg, " "))==0){
         	 *nb_copies = atoi(optarg);
      }
      break;
      case 'd':       /* temps d'arret */
         if (!(strcmp(optarg, " "))==0){
         	 *delay = atoi(optarg);
      }
      break;
      case 'l':       /* temps d'arret */
         if (!(strcmp(optarg, " "))==0){
         	 *lost =(double) atof(optarg);
      }
      break;
      case 'j':       /* temps d'arret */
           
     //  if ((strcmp(optarg, " "))==0){
         	 *rand = -1; //atof(optarg);
      //}
      break;
      case ':':
      if(optopt != 'j'){       /* missing operand */
      	printf(" %s Option - %c  Operand is mandatory \n",argv[0],optopt);
       	usageProg(argv[0], EXIT_FAILURE);
      }
      break;
      case '?':       /* unknown option */
      if(optopt != 'j'){
      	printf(" %s Unknown option %c \n",argv[0],optopt);
      	usageProg(argv[0], EXIT_FAILURE);
      }else
      //if(optopt == 'j')
      	*rand = -1;
    };
  };
  if(argc < 3){
  	usageProg(argv[0], EXIT_FAILURE);
  }
  else{
  		//strncpy (_r_host,argv[1],strlen(argv[1]));
  		//*_r_port = atoi(argv[2]);
  }
 
return 0;
}

int _parse(int argc, char *argv[],u_short *_l_port,char *format){
 	 extern char *optarg;
  extern int optind, opterr, optopt;
  
  
  char c;
   while ((c = getopt(argc, argv, "a:p:f:uh")) != EOF){
    switch (c) {
      case 'h':       /* Aide */
      used(argv[0], EXIT_SUCCESS);

      break;
      case 'p':
      if (!(strcmp(optarg, " "))==0){
          *_l_port = atoi(optarg);
      }
      break;
      case 'f':
      if (!(strcmp(optarg, " "))==0){
      	  strcpy (format,optarg);
          //*_l_port = atoi(optarg);
      }
      break;
      case ':':       /* missing operand */
      printf(" %s Option - %c  Operand is mandatory \n",argv[0],optopt);
       used(argv[0], EXIT_FAILURE);
      break;
      case '?':       /* unknown option */
      printf(" %s Unknown option %c \n",argv[0],optopt);
      used(argv[0], EXIT_FAILURE);
    };
  };
  
return 0;
 	
}

